package com.cf.chat.server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
 * @Auther: chenf
 * @Date: 2020/07/28/14:45
 * @Description:服务端
 */
public class Server {
    /**
     * 创建集合对象，存储每一个连接进来的客户端
     */
    public static List<MyChannel> list = new ArrayList<MyChannel>();
    public static void main(String[] args) throws IOException {
        System.out.println("----------------服务器端已开启---------------------");
        // 1.创建ServerSocket对象
        ServerSocket server = new ServerSocket(9999);
        // 2.监听客户端是否有客户端连接
        while (true) {
            Socket socket = server.accept();
            // 创建线程类的对象
            MyChannel channel=new MyChannel(socket);
            // 创建Thread类对象并启动线程
            Thread currentThread = new Thread(channel);
            currentThread.start();
            channel.setChannelName(currentThread.getName());
            // 添加到集合中
            list.add(channel);
            System.out.println("客户端" + currentThread.getName() + "已上线");
        }
        // 关闭流
        //CloseUtil.closeAll(dos, dis, socket);
    }
}

